<?php
class EtcController extends AppController
{
    public $name = 'Etc';
    public $helpers = array('Html', 'Form');
    public $components = array('Session');
    public $uses = array(
        'tbl_account', 'tbl_character', 'tbl_billing_log', 'tbl_item', 'tbl_card',
        'tbl_system_notice', 'tbl_event_table', 'tbl_gift_item_info'
    );

    public function event_setting()
    {
        if(!CakeSession::check('tbl_auth'))
        {
            $this->redirect(array(
                'controller' => 'User',
                'action' => 'user_list',
            ));
        }

        if( !empty($this->data) )
        {
            $event_num = $this->data['event_setting']['event_num'];
            $data = array(
                'login_evt_start_date' => $this->data['event_setting']['start_date'],
                'login_evt_end_date' => $this->data['event_setting']['end_date'],
                'login_evt_msg' => $this->data['event_setting']['message'],
                'login_evt_reward_data' => ''
            );

            for($i=1; $i<=5; ++$i)
            {
                if($this->data['event_setting']['gift_'.$i] == 'empty') {}
                else if($this->data['event_setting']['gift_'.$i] == 'item')
                {
                    $reward_data[] = array(
                        $this->data['event_setting']['gift_'.$i] => (int)$this->data['event_setting']['item_'.$i],
                        'count' => (int)$this->data['event_setting']['ea_'.$i]
                    );
                }
                else
                {
                    $reward_data[] = array(
                        $this->data['event_setting']['gift_'.$i] => (int)$this->data['event_setting']['ea_'.$i]
                    );
                }
            }
            $data['login_evt_reward_data'] = json_encode($reward_data);

            $this->tbl_event_table->id = $event_num;
            $this->tbl_event_table->save($data);
        }

        $event_table = $this->tbl_event_table->get_event();
        $gift_item_option = $this->__gift_item_option();

        $login_reward = json_decode($event_table['tbl_event_table']['login_evt_reward_data'], true);

        foreach($login_reward as $num => $value)
        {
            $key = array_keys($value);
            $login_reward[$num]['reward'] = $key[0];
        }

        $this->set(compact('event_table', 'gift_item_option', 'login_reward'));
    }

    public function system_setting()
    {
        if(!CakeSession::check('tbl_auth'))
        {
            $this->redirect(array(
                'controller' => 'User',
                'action' => 'user_list',
            ));
        }

        if( !empty($this->data) )
        {
            $system = $this->data['system_setting'];

            if( !dateCheck($system['start_date']) || !dateCheck($system['end_date']) )
            {
                $err_msg = __('The date format must be!');
            }
            else
            {
                $this->tbl_system_notice->save($system);
                $sus_msg = __('Setting Succeeded!!');
            }
        }
        
        $server = $this->tbl_system_notice->get_server_status();

        $this->set(compact('server', 'sus_msg', 'err_msg'));
    }

    public function server_state()
    {
        $this->layout = 'server';
        if( !empty($this->data) )
        {
            $system = $this->data['system_setting'];
            $this->tbl_system_notice->save($system);
        }
        $server = $this->tbl_system_notice->get_server_status();
        $this->set(compact('server', 'sus_msg', 'err_msg'));
    }

    private function __gift_item_option()
    {
        $gift_item_list = $this->tbl_gift_item_info->get_gift_item();

        foreach($gift_item_list as $item)
        {
            $gift_item[$item['tbl_gift_item_info']['item_id']] = $item['tbl_gift_item_info']['item_name'];
        }

        return $gift_item;
    }
}